from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

from haoshuyun_seleinum import biz_mysql


def haoshuyun_seleinum(Novel_name, insert_time, Chapter, Extension_name, Platform):
    driver = webdriver.Chrome()
    driver.get("http://book.zhihaoyun.com/novel/user/publics/signin")
    driver.maximize_window()
    driver.implicitly_wait(20)

    # 定位账号
    driver.find_element_by_xpath('//*[@id="login-username"]').clear()
    driver.find_element_by_xpath('//*[@id="login-username"]').send_keys(username)
    # 定位密码
    driver.find_element_by_xpath('//*[@id="login-password"]').clear()
    driver.find_element_by_xpath('//*[@id="login-password"]').send_keys(password)
    # 点击登录
    driver.find_element_by_xpath('//button[text()="登 录"]').click()

    # 弹出框
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="layui-layer1"]/span[1]/a'))
        )
        time.sleep(1)
        driver.find_element_by_xpath('//*[@id="layui-layer1"]/span[1]/a').click()
    except Exception:
        print("点击 弹出框1")

    try:
        time.sleep(0.5)
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="layui-layer2"]/div[2]/a'))
        )
        driver.find_element_by_xpath('//*[@id="layui-layer2"]/div[2]/a').click()
    except Exception:
        print("点击 弹出框2")

    # 小说列表
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//span[text()="小说列表"]'))
        )
        driver.find_element_by_xpath('//span[text()="小说列表"]').click()
    except Exception:
        print("点击 小说列表")

    # 输入名称
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="search-input"]'))
        )
        driver.find_element_by_xpath('//*[@id="search-input"]').clear()
        driver.find_element_by_xpath('//*[@id="search-input"]').send_keys(Novel_name)
    except Exception:
        print("点击 输入名称")

    # 搜索
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="ss_btn"]'))
        )
        driver.find_element_by_xpath('//*[@id="ss_btn"]').click()
    except Exception:
        print("点击 搜索")

    # 点击小说
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="main-container"]//td[2]/a'))
        )
        driver.find_element_by_xpath('//*[@id="main-container"]//td[2]/a').click()
    except Exception:
        print("点击 小说名")

    # 新窗口
    driver.switch_to.window(driver.window_handles[-1])

    # 获取推广链接
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="main-container"]//tr[2]//a[2]'))
        )
        driver.find_element_by_xpath('//*[@id="main-container"]//tr[2]//a[2]').click()
    except Exception:
        print("点击 获取推广链接")

    # 渠道名称
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="name"]'))
        )
        driver.find_element_by_xpath('//*[@id="name"]').clear()
        driver.find_element_by_xpath('//*[@id="name"]').send_keys(str(insert_time) + str(Extension_name))
    except Exception:
        print("点击 渠道名称")

    # 提交
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//button[@class="btn btn-minw btn-primary ajax-post"]'))
        )
        driver.find_element_by_xpath('//button[@class="btn btn-minw btn-primary ajax-post"]').click()
    except Exception:
        print("点击 提交")

    # url
    try:
        WebDriverWait(driver, 60).until(
            EC.element_to_be_clickable((By.XPATH, '//tr[1]/td[1]/span[2]/button[1]'))
        )
        url = driver.find_element_by_xpath(
            '//tr[1]/td[1]/span[2]/button[1]').get_attribute("data-clipboard-text")
        print(url)
        # 数据入库
        try:
            biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name,
                                         Chapter=Chapter, datetime=str(insert_time),
                                         Extension_name=str(Extension_name))
        except Exception:
            print("数据入库失败")
    except Exception:
        print("获取 url")

    driver.quit()
